package com.teradyne.merlin2.ui.client.views;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.AsyncHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.teradyne.merlin2.ui.client.resources.MerlinStyle;
import com.teradyne.merlin2.ui.shared.models.RequestStatus;

public class OrderListViewImpl extends Composite implements OrderListView {

	private static OrderListViewImplUiBinder uiBinder = GWT.create(OrderListViewImplUiBinder.class);

	interface OrderListViewImplUiBinder extends UiBinder<Widget, OrderListViewImpl> {
	}

	@UiField(provided = true)
	CellTable<RequestStatus> dataGrid;

	@UiField(provided = true)
	SimplePager simplePager;

	private Presenter presenter;
	private MerlinStyle merlinStyle;

	@Inject
	public OrderListViewImpl(SimplePager.Resources pagerResources, MerlinStyle merlinStyle) {
		this.merlinStyle = merlinStyle;

		dataGrid = new CellTable<RequestStatus>();
		simplePager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);

		initWidget(uiBinder.createAndBindUi(this));

		buildDataGrid();

	}

	private void buildDataGrid() {

		TextColumn<RequestStatus> typeColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getType();
			}
		};
		dataGrid.addColumn(typeColumn, "Type");

		TextColumn<RequestStatus> contactColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getContact();
			}
		};
		dataGrid.addColumn(contactColumn, "Contact");

		TextColumn<RequestStatus> referenceNoColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getReferenceNo();
			}
		};

		dataGrid.addColumn(referenceNoColumn, "Reference No");

		TextColumn<RequestStatus> lineNoColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getLineNo();
			}
		};

		dataGrid.addColumn(lineNoColumn, "Line No");

		TextColumn<RequestStatus> partNoColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getPartNo();
			}
		};

		dataGrid.addColumn(partNoColumn, "Part No");

		TextColumn<RequestStatus> partSerialNoColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getPartSerialNo();
			}
		};

		dataGrid.addColumn(partSerialNoColumn, "Part Serial No");

		TextColumn<RequestStatus> quantityColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getQuantity();
			}
		};

		dataGrid.addColumn(quantityColumn, "Quantity");

		TextColumn<RequestStatus> shipmentNoColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getShipmentNo();
			}
		};

		dataGrid.addColumn(shipmentNoColumn, "Shipment No");

		TextColumn<RequestStatus> serviceTypeColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getServiceType();
			}
		};

		serviceTypeColumn.setSortable(true);
		dataGrid.addColumn(serviceTypeColumn, "Service Type");

		TextColumn<RequestStatus> dateCreatedColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getDateCreated();
			}
		};

		dataGrid.addColumn(dateCreatedColumn, "Date Created");

		TextColumn<RequestStatus> statusColumn = new TextColumn<RequestStatus>() {
			@Override
			public String getValue(RequestStatus object) {
				return object.getStatus();
			}
		};

		dataGrid.addColumn(statusColumn, "Status");

		simplePager.setDisplay(dataGrid);

		AsyncHandler columnSortHandler = new AsyncHandler(dataGrid);
		dataGrid.addColumnSortHandler(columnSortHandler);
		dataGrid.getColumnSortList().push(serviceTypeColumn);

	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public CellTable<RequestStatus> getOrderListTable() {
		return dataGrid;
	}

}
